/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Periodo;

import DBMS.DataBaseManager;
import clases.Switch;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import pio.action.Printer;

/**
 *
 * @author andreth
 */
public class GestionarPeriodo extends DispatchAction {

    private final String SUCCESS = "success";

    public ActionForward page(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        if (request.getSession().getAttribute("Switch") == null) {
            System.out.println("HOLA ENTRANDO AL PAGE ----------------------------");
            Switch s;
            s = DataBaseManager.getInstance().consultarSwitch();
            request.setAttribute("Switch", s);
        }
        return mapping.findForward(SUCCESS);
    }

    public ActionForward save(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Switch s = (Switch) form;
        Boolean agregado;
        agregado = DataBaseManager.agregarSwitch(s);
        if (agregado) {
            s.setMensaje("Periodo Agregado");
            return mapping.findForward(SUCCESS);
        } else {
            s.setMensaje("Formato Invalido");
            Printer.print("Switch invalido en agregar"
                    + s.toStringAtts()
                    + s.toStringValues(), "Gestionar Periodo");
            request.getSession().setAttribute("Switch", s);
            return mapping.findForward(SUCCESS);
        }
    }
}
